package com.zft.userwalletmanager.web.controller;

import com.zft.userwalletmanager.application.handler.WalletHandler;
import com.zft.userwalletmanager.domain.bo.WalletBO;
import com.zft.userwalletmanager.infrastructure.common.enums.CashFlowEnums;
import com.zft.userwalletmanager.infrastructure.common.enums.ResultCode;
import com.zft.userwalletmanager.infrastructure.common.enums.TransferTypeEnums;
import com.zft.userwalletmanager.infrastructure.common.exception.BizException;
import com.zft.userwalletmanager.infrastructure.common.utils.ValidationUtils;
import com.zft.userwalletmanager.web.Result;
import com.zft.userwalletmanager.web.request.WalletBalanceRequest;
import com.zft.userwalletmanager.web.request.WalletCreditRequest;
import com.zft.userwalletmanager.web.request.WalletRefundRequest;
import com.zft.userwalletmanager.web.response.WalletBalanceResponse;
import com.zft.userwalletmanager.web.response.WalletCreditResponse;
import com.zft.userwalletmanager.web.response.WalletRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping(value = "/wallet")
public class WalletController {
    @Autowired
    private WalletHandler walletHandler;

    /**
     * 获取钱包余额
     *
     * @param walletBalanceRequest
     * @return
     */
    @RequestMapping(value = "/getBalance", method = RequestMethod.POST)
    public Result<WalletBalanceResponse> getBalance(@RequestBody WalletBalanceRequest walletBalanceRequest) {
        ValidationUtils.validate(walletBalanceRequest);
        WalletBO walletBO = new WalletBO();
        walletBO.setUserId(walletBalanceRequest.getUserId());
        Long balance = walletHandler.getBalance(walletBO);
        WalletBalanceResponse walletBalanceResponse = new WalletBalanceResponse();
        walletBalanceResponse.setWalletBalance(balance);
        return Result.success(walletBalanceResponse);
    }

    /**
     * 用户消费接口
     *
     * @param walletCreditRequest
     * @return
     */
    @RequestMapping(value = "/credit", method = RequestMethod.GET)
    public Result<WalletCreditResponse> credit(@RequestBody WalletCreditRequest walletCreditRequest) {
        ValidationUtils.validate(walletCreditRequest);
        if (walletCreditRequest.getUserId().equals(walletCreditRequest.getCounterPartyWalletNo())) {
            throw new BizException(ResultCode.WALLET_INFO_IS_NULL);
        }
        WalletBO walletBO = new WalletBO();
        walletBO.setOrderId(walletCreditRequest.getOrderId());
        walletBO.setUserId(walletCreditRequest.getUserId());
        walletBO.setAmount(walletCreditRequest.getAmount());
        walletBO.setCurrency(walletCreditRequest.getWalletCurrency());
        walletBO.setType(TransferTypeEnums.CREDIT.name());
        walletBO.setCashFlow(CashFlowEnums.OUT.name());
        walletBO.setCounterPartyWalletNo(walletCreditRequest.getCounterPartyWalletNo());
        walletHandler.credit(walletBO);
        WalletCreditResponse walletCreditResponse = new WalletCreditResponse();
        walletCreditResponse.setOrderId(walletCreditRequest.getOrderId());
        return Result.success(walletCreditResponse);
    }

    /**
     * 退款接口
     * @param walletRefundRequest
     * @return
     */
    @RequestMapping(value = "/refund", method = RequestMethod.GET)
    public Result<WalletRefundResponse> refund(@RequestBody WalletRefundRequest walletRefundRequest) {
        ValidationUtils.validate(walletRefundRequest);
        if (walletRefundRequest.getUserId().equals(walletRefundRequest.getCounterPartyWalletNo())) {
            throw new BizException(ResultCode.ACCOUNT_IS_SAME);
        }
        WalletBO walletBO = new WalletBO();
        walletBO.setOrderId(walletRefundRequest.getOrderId());
        walletBO.setUserId(walletRefundRequest.getUserId());
        walletBO.setAmount(walletRefundRequest.getRefund());
        walletBO.setType(TransferTypeEnums.DEBIT.name());
        walletBO.setCurrency(walletRefundRequest.getCurrency());
        walletBO.setCashFlow(CashFlowEnums.IN.name());
        walletBO.setCounterPartyWalletNo(walletRefundRequest.getCounterPartyWalletNo());
        walletHandler.debit(walletBO);
        WalletRefundResponse walletRefundResponse = new WalletRefundResponse();
        walletRefundResponse.setOrderId(walletRefundRequest.getOrderId());
        return Result.success(walletRefundResponse);
    }
}
